package com.lvjunxiong.serviceinterface.task;

import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.lvjunxiong.serviceinterface.ServiceDao;
import com.lvjunxiong.serviceinterface.Serviceutil;
import com.lvjunxiong.serviceinterface.tongxun.*;

import common.CommModel;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.log4j.Logger;
import org.codehaus.jackson.map.ObjectMapper;

import java.net.HttpURLConnection;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

import static com.lvjunxiong.serviceinterface.Serviceutil.doPost;

/**
 * Created by lvjunxiong on 2018/11/11.
 */
public class Lock_TaskToken3 extends TimerTask {
	private static final Logger logger = Logger
			.getLogger(Lock_TaskToken3.class);
	private ServiceDao dao = new ServiceDao();

	public static void main(String[] args) {
		logger.info("=========开始lock_token+++++++++++++++++++++监听=========");
		Timer timer = new Timer();
		// timer.scheduleAtFixedRate(new Lock_TaskToken3(), 1, 5 * 1000);
		Lock_TaskToken l = new Lock_TaskToken();
		l.run();
	}

	@Override
	public void run() {
		dao.deletesh_suoyin();
		dao.deletesh_suoyin_list();
	
		ObjectMapper objectMapper = new ObjectMapper();
		HttpURLConnection conn = null;
		String all_tongxunlist = null;
		try {
			all_tongxunlist = doPost(
					"http://app.shuhai.cc/isms/manager/party/all/4028f69f55304e0b01553279b6310042",
					"");
		} catch (Exception e) {
			e.printStackTrace();
		}
		System.out.println(all_tongxunlist);
		JSONObject jo_ = JSONObject.fromObject(all_tongxunlist);
		JSONArray rows = jo_.getJSONArray("rows");
		TongxunDao dao = new TongxunDao();
		TongxunModel model = new TongxunModel();
		List<Tongxun_twoModel> tongxunlist = new ArrayList<Tongxun_twoModel>();
		List<Sh_groupname> list_ = Sh_groupname.getAInitSh_groupnameList();
		Collections.sort(list_, new GroupnameComparetor());

		for (int i = 0; i < list_.size(); i++) {
			Sh_groupname kk = list_.get(i);
			String group_name = kk.getGroupName();
			Tongxun_twoModel mm = new Tongxun_twoModel();
			mm.setId(group_name);
			tongxunlist.add(mm);
		}
		JSONArray jsons = new JSONArray();
		for (int i = 0; i < rows.size(); i++) {
			String res = rows.getString(i);
			JsonParser jps = new JsonParser();
			JsonObject jos = jps.parse(res).getAsJsonObject();
			String gain_id = jos.get("id").getAsString();// 获取的id
			String gain_name = jos.get("name").getAsString();// 获取的姓名
			String type = jos.get("type").getAsString();// 获取的类型
			String parentId = jos.get("parentId").getAsString();// 父ID
			String res_ = Serviceutil.converterToFirstSpell(gain_name);
			if ((!type.equals("PARTY_GROUP"))
					| parentId.equals("4028f69f55304e0b01553279b6310042")) {
				jsons.add(res);
			}
		}
		List<Tongxun_twoModel> list = new ArrayList<Tongxun_twoModel>();
		for (int j = 0; j < tongxunlist.size(); j++) {
			Tongxun_twoModel mm = tongxunlist.get(j);
			String groupName = mm.getId();
			System.out.println(groupName + "====获取到的游标");
			dao.insertSh_suoyin(groupName);
			List<Tongxun_twoListModel> users = new ArrayList<Tongxun_twoListModel>();

			for (int m = 0; m < jsons.size(); m++) {
				String res1 = jsons.getString(m);
				JsonParser jps1 = new JsonParser();
				JsonObject jos1 = jps1.parse(res1).getAsJsonObject();
				String gain_id1 = jos1.get("id").getAsString();// 获取的id
				String gain_name1 = jos1.get("name").getAsString();// 获取的姓名
				String type1 = jos1.get("type").getAsString();// 获取的类型
				String parentId1 = jos1.get("parentId").getAsString();// 父ID
				String res_1 = Serviceutil.converterToFirstSpell(gain_name1);
				if (res_1.length() > 0) {
					String zhuanhua_res = res_1.substring(0, 1);
					if (groupName.equals(zhuanhua_res)) {
						System.out.println("==获取到的id==" + gain_id1
								+ "==获取到的姓名==" + gain_name1 + "==获取到的首字母是=="
								+ zhuanhua_res);
						Tongxun_twoListModel tongxunListModel = new Tongxun_twoListModel();
						tongxunListModel.setId(gain_id1);
						tongxunListModel.setName(gain_name1);
						int fg = dao.insertSh_suoyin_list(gain_id1, gain_name1,
								zhuanhua_res, type1);

						users.add(tongxunListModel);
						mm.setItems(users);
					}
				} else {
					logger.info("不做任何操作，STOP");
				}
			}
			if (users.size() > 0) {
				mm.setEnabled(true);
			} else {
				mm.setEnabled(false);
			}
			List<CommModel> fff = dao.queryYB(groupName);
			if (fff.size() > 0) {
				int i = dao.updatesh_suoyin(groupName, "true");
				if (i > 0) {
					logger.info("更新索引true成功");
				} else {
					logger.info("更新索引true失败");
				}
			} else {
				int i = dao.updatesh_suoyin(groupName, "false");
				if (i > 0) {
					logger.info("更新索引false成功");
				} else {
					logger.info("更新索引false失败");
				}
			}
			list.add(mm);
		}
		dao.delOther();
	}
}